const contents = document.querySelectorAll('.content')

for (let item of contents) {
  // console.log(item)
  const zan = item.querySelector('.zan p:nth-child(1)')
  zan.addEventListener('click', function () {
    let isZan = this.children[0].getAttribute('data-zan')
    if (isZan === '1') { // 已经点赞
      this.children[0].src = './pic/state-thumbs-up.svg'
      this.children[0].setAttribute('data-zan', '0')
      this.children[1].innerHTML -= 1
    } else { // 没点赞
      this.children[0].src = './pic/hearts.svg'
      this.children[0].setAttribute('data-zan', '1')
      this.children[1].innerHTML = +this.children[1].innerHTML + 1
    }
  })


  // 初始化评论
  const comments = item.querySelectorAll('.comment p')
  const commentBtn = item.querySelector('.comment span')
  for (let i = 0; i < comments.length; i++) {
    if (i >= 3) {
      comments[i].style.display = 'none'
    }
  }
  if (comments.length > 3) {
    const btnText= commentBtn.querySelector('i')
    const btnImg = commentBtn.querySelector('img')
    btnText.innerHTML = `查看全部${comments.length}条内容`
    btnImg.classList.remove('up')
  } else {
    if (commentBtn) {
      commentBtn.remove()
    }
  }

  // 展开收起评论
  if (commentBtn) {
    commentBtn.addEventListener('click', function () {
      const btnText= commentBtn.querySelector('i')
      const btnImg = commentBtn.querySelector('img')

      if (btnImg.classList.contains('up')) {
        for (let i = 0; i < comments.length; i++) {
          if (i >= 3) {
            comments[i].style.display = 'none'
          }
        }
        btnText.innerHTML = `查看全部${comments.length}条内容`
        btnImg.classList.remove('up')
      } else { // 没有展开
        for (let i = 0; i < comments.length; i++) {
          comments[i].style.display = 'block'
        }
        btnText.innerHTML = '收起'
        btnImg.classList.add('up')
      }
    })
  }

  // console.log(comments.length)
  // console.log(commentBtn)
}


